/*     */ package dm.jdbc.driver;
/*     */ 
/*     */ import dm.jdbc.dbaccess.Const;
/*     */ import java.io.IOException;
/*     */ import java.io.Reader;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ResourceBundle;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DmdbReader
/*     */   extends Reader
/*     */ {
/*  29 */   private DmdbBlob blob = null;
/*     */   
/*  31 */   private DmdbClob clob = null;
/*     */   
/*     */   private int typeFlag;
/*     */   
/*  35 */   private String stringBuffer = null;
/*     */   
/*  37 */   byte[] byteBuffer = null;
/*     */   
/*  39 */   char[] charBuffer = null;
/*     */   
/*  41 */   int currentPos = 0;
/*     */   
/*     */   static final int blobLen = 32000;
/*     */   
/*     */   static final int clobLen = 8000;
/*  46 */   int curPos = 0;
/*     */   
/*     */   int byteLen;
/*     */   
/*     */   int charLen;
/*     */   
/*  52 */   private boolean closed = false;
/*     */   
/*     */ 
/*     */ 
/*  56 */   long lob_length = -1L;
/*     */   
/*     */   public DmdbReader(Object o) throws NumberFormatException {
/*  59 */     if ((o instanceof DmdbBlob)) {
/*  60 */       this.blob = ((DmdbBlob)o);
/*  61 */       this.typeFlag = 0;
/*  62 */     } else if ((o instanceof DmdbClob)) {
/*  63 */       this.clob = ((DmdbClob)o);
/*  64 */       this.typeFlag = 1;
/*  65 */     } else if ((o instanceof String)) {
/*  66 */       this.typeFlag = 2;
/*  67 */       this.stringBuffer = ((String)o);
/*  68 */     } else if ((o instanceof byte[])) {
/*  69 */       this.typeFlag = 3;
/*  70 */       this.byteBuffer = ((byte[])o);
/*     */     } else {
/*  72 */       throw new NumberFormatException("cast error.");
/*     */     }
/*     */   }
/*     */   
/*     */   protected DmdbReader(DmdbClob clob, long pos, long length) {
/*  77 */     this.typeFlag = 1;
/*  78 */     this.clob = clob;
/*  79 */     this.currentPos = ((int)(pos - 1L));
/*  80 */     this.lob_length = length;
/*     */   }
/*     */   
/*     */   public int read(char[] dest, int destOff, int readLen) throws IOException
/*     */   {
/*  85 */     if (this.closed) {
/*  86 */       throw new IOException(
/*  87 */         Const.res.getString("error.ClosedStream"));
/*     */     }
/*  89 */     if (dest == null) {
/*  90 */       throw new NullPointerException();
/*     */     }
/*  92 */     if ((destOff < 0) || (readLen < 0) || (destOff + readLen > dest.length)) {
/*  93 */       throw new IndexOutOfBoundsException(
/*  94 */         Const.res.getString("error.IndexOutOfBound"));
/*     */     }
/*  96 */     switch (this.typeFlag) {
/*     */     case 0: 
/*  98 */       return readFromBlob(dest, destOff, readLen);
/*     */     
/*     */     case 1: 
/* 101 */       return readFromClob(dest, destOff, readLen);
/*     */     
/*     */     case 2: 
/* 104 */       return readFromString(dest, destOff, readLen);
/*     */     
/*     */     case 3: 
/* 107 */       return readFromByteArray(dest, destOff, readLen);
/*     */     }
/*     */     
/* 110 */     throw new IOException("io read error.");
/*     */   }
/*     */   
/*     */   public void close() throws IOException
/*     */   {
/* 115 */     this.closed = true;
/*     */   }
/*     */   
/*     */   private int readFromBlob(char[] dest, int destOff, int readLen)
/*     */     throws IOException
/*     */   {
/* 121 */     int retLen = 0;
/*     */     
/*     */     try
/*     */     {
/* 125 */       if (this.blob == null) {
/* 126 */         return -1;
/*     */       }
/*     */       
/* 129 */       if (this.lob_length < 0L) {
/* 130 */         this.lob_length = this.blob.length();
/*     */       }
/* 132 */       if (((this.byteBuffer == null) || ((this.byteLen = this.byteBuffer.length) <= this.curPos)) && 
/* 133 */         (this.lob_length <= this.currentPos)) {
/* 134 */         return -1;
/*     */       }
/*     */       
/* 137 */       long invalidLen = 0L;
/* 138 */       if (this.byteBuffer == null) {
/* 139 */         invalidLen = this.lob_length - this.currentPos;
/*     */       } else {
/* 141 */         invalidLen = this.lob_length - this.currentPos + this.byteLen - this.curPos;
/*     */       }
/*     */       
/* 144 */       int destLen = (int)(invalidLen > readLen ? readLen : invalidLen);
/*     */       
/*     */ 
/* 147 */       while (retLen < destLen)
/*     */       {
/* 149 */         if ((this.byteBuffer == null) || 
/* 150 */           ((this.byteLen = this.byteBuffer.length) <= this.curPos)) {
/* 151 */           int bufLen = (int)(this.lob_length - this.currentPos > 32000L ? 32000L : 
/* 152 */             this.lob_length - this.currentPos);
/*     */           
/* 154 */           this.byteBuffer = this.blob.getBytes(this.currentPos + 1, bufLen);
/*     */           
/* 156 */           if ((this.byteBuffer != null) && (this.byteBuffer.length > 0)) {
/* 157 */             this.curPos = 0;
/* 158 */             this.byteLen = bufLen;
/*     */             
/* 160 */             this.currentPos += bufLen;
/*     */           } else {
/* 162 */             this.byteBuffer = new byte[0];
/*     */             
/* 164 */             this.curPos = 0;
/* 165 */             this.byteLen = bufLen;
/*     */             
/* 167 */             break;
/*     */           }
/*     */         }
/*     */         
/*     */ 
/* 172 */         int realLen = destLen - retLen < this.byteLen - this.curPos ? destLen - 
/* 173 */           retLen : this.byteLen - this.curPos;
/* 174 */         for (int i = 0; i < realLen; i++) {
/* 175 */           dest[(destOff + i)] = ((char)this.byteBuffer[(this.curPos + i)]);
/*     */         }
/* 177 */         destOff += realLen;
/* 178 */         this.curPos += realLen;
/* 179 */         retLen += realLen;
/*     */       }
/*     */     } catch (SQLException sqle) {
/* 182 */       throw new IOException(sqle.getMessage());
/*     */     }
/*     */     
/* 185 */     return retLen;
/*     */   }
/*     */   
/*     */   private int readFromClob(char[] dest, int destOff, int readLen)
/*     */     throws IOException
/*     */   {
/* 191 */     int retLen = 0;
/*     */     
/*     */     try
/*     */     {
/* 195 */       if (this.clob == null) {
/* 196 */         return -1;
/*     */       }
/*     */       
/* 199 */       if (this.lob_length < 0L) {
/* 200 */         this.lob_length = this.clob.length();
/*     */       }
/* 202 */       if (((this.charBuffer == null) || ((this.charLen = this.charBuffer.length) <= this.curPos)) && 
/* 203 */         (this.lob_length <= this.currentPos)) {
/* 204 */         return -1;
/*     */       }
/*     */       
/* 207 */       long invalidLen = 0L;
/* 208 */       if (this.charBuffer == null) {
/* 209 */         invalidLen = this.lob_length - this.currentPos;
/*     */       } else {
/* 211 */         invalidLen = this.lob_length - this.currentPos + this.charLen - this.curPos;
/*     */       }
/*     */       
/* 214 */       int destLen = (int)(invalidLen > readLen ? readLen : invalidLen);
/*     */       
/*     */ 
/* 217 */       while (retLen < destLen)
/*     */       {
/* 219 */         if ((this.charBuffer == null) || 
/* 220 */           ((this.charLen = this.charBuffer.length) <= this.curPos)) {
/* 221 */           int bufLen = (int)(this.lob_length - this.currentPos > 8000L ? 8000L : 
/* 222 */             this.lob_length - this.currentPos);
/*     */           
/* 224 */           String subString = this.clob
/* 225 */             .getSubString(this.currentPos + 1, bufLen);
/*     */           
/* 227 */           if ((subString != null) && (subString.length() > 0)) {
/* 228 */             this.charBuffer = new char[bufLen];
/* 229 */             subString.getChars(0, bufLen, this.charBuffer, 0);
/*     */             
/* 231 */             this.curPos = 0;
/* 232 */             this.currentPos += bufLen;
/* 233 */             this.charLen = bufLen;
/*     */           } else {
/* 235 */             this.charBuffer = new char[0];
/* 236 */             this.curPos = 0;
/* 237 */             this.charLen = 0;
/*     */             
/* 239 */             break;
/*     */           }
/*     */         }
/*     */         
/*     */ 
/* 244 */         int realLen = destLen - retLen < this.charLen - this.curPos ? destLen - 
/* 245 */           retLen : this.charLen - this.curPos;
/* 246 */         System.arraycopy(this.charBuffer, this.curPos, dest, destOff, realLen);
/*     */         
/* 248 */         destOff += realLen;
/* 249 */         this.curPos += realLen;
/* 250 */         retLen += realLen;
/*     */       }
/*     */     } catch (SQLException sqle) {
/* 253 */       throw new IOException(sqle.getMessage());
/*     */     }
/*     */     
/* 256 */     return retLen;
/*     */   }
/*     */   
/*     */ 
/*     */   private int readFromString(char[] dest, int destOff, int readLen)
/*     */     throws IOException
/*     */   {
/* 263 */     if ((this.stringBuffer == null) || ((this.charLen = this.stringBuffer.length()) <= this.curPos)) {
/* 264 */       return -1;
/*     */     }
/*     */     
/* 267 */     int invalidLen = readLen > this.charLen - this.curPos ? this.charLen - this.curPos : 
/* 268 */       readLen;
/* 269 */     if (destOff + invalidLen > dest.length) {
/* 270 */       throw new IndexOutOfBoundsException(
/* 271 */         Const.res.getString("error.IndexOutOfBound"));
/*     */     }
/* 273 */     this.stringBuffer.getChars(this.curPos, this.curPos + invalidLen, dest, destOff);
/*     */     
/*     */ 
/* 276 */     this.curPos += invalidLen;
/*     */     
/* 278 */     return invalidLen;
/*     */   }
/*     */   
/*     */ 
/*     */   private int readFromByteArray(char[] dest, int destOff, int readLen)
/*     */     throws IOException
/*     */   {
/* 285 */     if ((this.byteBuffer == null) || ((this.byteLen = this.byteBuffer.length) <= this.curPos)) {
/* 286 */       return -1;
/*     */     }
/*     */     
/* 289 */     int invalidLen = readLen > this.byteLen - this.curPos ? this.byteLen - this.curPos : 
/* 290 */       readLen;
/*     */     
/* 292 */     if (destOff + invalidLen > dest.length) {
/* 293 */       throw new IndexOutOfBoundsException(
/* 294 */         Const.res.getString("error.IndexOutOfBound"));
/*     */     }
/* 296 */     for (int i = 0; i < invalidLen; i++) {
/* 297 */       dest[(destOff + i)] = ((char)this.byteBuffer[(i + this.curPos)]);
/*     */     }
/*     */     
/* 300 */     this.curPos += invalidLen;
/*     */     
/* 302 */     return invalidLen;
/*     */   }
/*     */ }


/* Location:              D:\Program Files\DBMS_20150813\jdbc\Dm7JdbcDriver17.jar!\dm\jdbc\driver\DmdbReader.class
 * Java compiler version: 2 (46.0)
 * JD-Core Version:       0.7.1
 */